package com.gxuwz.medical.domain.paybase;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.exception.DbException;

public class PayBase {
	private String id;
	private String year;
	private String money;
	private Timestamp startTime;
	private Timestamp endTime;

	public PayBase() {
		super();
	}

	public PayBase(String id, String year, String money, Timestamp startTime, Timestamp endTime) {
		super();
		this.id = id;
		this.year = year;
		this.money = money;
		this.startTime = startTime;
		this.endTime = endTime;
	}

	/**
	 * 实例化对象
	 * 
	 * @param areacode
	 * @throws DbException
	 */
	public PayBase(String id) throws DbException {
		if (id == null) {
			throw new IllegalStateException("家庭档案信息编号不能空");
		}
		try {
			this.id = id;
			loadDB();
		} catch (SQLException e) {
			throw new DbException("加载id=" + this.getId() + "家庭档案信息记录失败", e);
		}
	}

	/**
	 * 
	 * @param areaid
	 * @throws SQLException
	 */
	private void loadDB() throws SQLException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DbUtil.getConn();
			pstmt = conn.prepareStatement("select * from t_paybase where id=?");
			int index = 1;
			pstmt.setString(index, this.getId());
			rs = pstmt.executeQuery();
			if (rs.next()) {
				this.id = rs.getString("id");
				this.year = rs.getString("year");
				this.money = rs.getString("money");
				this.startTime = rs.getTimestamp("startTime");
				this.endTime = rs.getTimestamp("endTime");
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(rs, pstmt, conn);
		}
	}

	public void edit() throws SQLException {
		Connection conn = DbUtil.getConn();
		try {
			conn.setAutoCommit(false);
			this.updateToDB(conn);
			conn.commit();
		} catch (SQLException e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	private void updateToDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		System.out.println("upDB=" + id);
		try {
			// 定义构造update SQL语句字符串变量sql
			String sql = "update t_paybase set year=?,money=?,startTime=?,endTime=? where id=?";
			// 创建执行带动态参数的SQL的PreparedStatement pstmt
			pstmt = conn.prepareStatement(sql);
			// 设置动态参数对应的值
			int index = 1;
			pstmt.setString(index++, this.year);
			pstmt.setString(index++, this.money);
			pstmt.setTimestamp(index++, this.startTime);
			pstmt.setTimestamp(index++, this.endTime);

			pstmt.setString(index++, this.id);
			int count = pstmt.executeUpdate();

		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(pstmt);
		}

	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getMoney() {
		return money;
	}

	public void setMoney(String money) {
		this.money = money;
	}

	public Timestamp getStartTime() {
		return startTime;
	}

	public void setStartTime(Timestamp startTime) {
		this.startTime = startTime;
	}

	public Timestamp getEndTime() {
		return endTime;
	}

	public void setEndTime(Timestamp endTime) {
		this.endTime = endTime;
	}

}
